class MyRobotDBR extends egret.DisplayObjectContainer {

    private EARTHQUAKE_EVENT_NAME:string = 'quake';
    private shakeTimer:egret.Timer = new egret.Timer(34, 2);
    private earquakeOffset:number = 4;

    public constructor() {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.addToStage, this);
        this.shakeTimer.addEventListener(egret.TimerEvent.TIMER, this.shake, this);
        this.shakeTimer.addEventListener(egret.TimerEvent.TIMER_COMPLETE, ()=>{
            this.shakeTimer.reset();
        }, this);
    }

    public async addToStage(event:egret.Event) {
        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onGroupComplete, this);

        await RES.loadConfig("resource/default.res.json", "resource/");
        await RES.loadGroup("preload");
    }

    public onGroupComplete(event:egret.Event) {
        var bgTexture:egret.Texture = RES.getRes('bg2_jpg');
        var bg:egret.Bitmap = new egret.Bitmap(bgTexture);
        this.addChild(bg);

        var vanessaTexture:egret.Texture = RES.getRes('vanessa_json#fist0');
        var vanessa:egret.Bitmap = new egret.Bitmap(vanessaTexture);
        this.addChild(vanessa);
        vanessa.anchorOffsetY = vanessa.height;
        vanessa.x = 100;
        vanessa.y = 570;

        var robotDragonbonesData = RES.getRes('robot_3_ske_json');
        var robotData = RES.getRes('robot_3_tex_json');
        var robotTexture = RES.getRes('robot_3_tex_png');

        let egretFactory: dragonBones.EgretFactory = dragonBones.EgretFactory.factory;
        egretFactory.parseDragonBonesData(robotDragonbonesData);  
        egretFactory.parseTextureAtlasData(robotData, robotTexture);
        let robot: dragonBones.EgretArmatureDisplay = egretFactory.buildArmatureDisplay("robot");
        
        this.addChild(robot);

        /* 动画事件监听 */
        robot.addEventListener(dragonBones.EventObject.FRAME_EVENT, this.animationEventHandler, this);

        /* 动画播放 */
        robot.x = 550;
        robot.y = 570;
        robot.scaleX = 0.3;
        robot.scaleY = 0.3;
        // robot.animation.play('walk_pre');
        // robot.animation.play('walk_pre').timeScale = 0.1;

        var testBtn = new eui.Button();
        testBtn.label = 'sdfsdfsdf';
        testBtn.x = 50;
        testBtn.y = 100;
        this.addChild(testBtn);

        
    }

    private animationEventHandler(event: dragonBones.EgretEvent):void {
        if( event.eventObject.name === this.EARTHQUAKE_EVENT_NAME ) {
            this.earthquake()
        }
    }

    public earthquake() {
        this.shakeTimer.start();
    }

    private shake() {
        if(this.y === 0) {
            //图像未震动
            this.y += this.earquakeOffset;
        }else {
            //图像震动复位
            this.y = 0;
        }
    }
}